import SM4 from 'sm-crypto/src/sm4';

const aesKey = import.meta.env.VITE_APP_ENCRYPT_KEY;

/**
 * 加密
 * @param data
 * @returns {*|string|[]}
 */
export const encryptSM4 = (data) => {
    return SM4.encrypt(data, aesKey);
};

/**
 * 解密
 * @param data
 * @returns {*|string|[]}
 */
export const decryptSM4 = (data) => {
    return SM4.decrypt(data, aesKey);
};


const key = 'abcde0123456789ffedcba9876543210';
const iv = 'fedcba987567894abcdef65432100123';
/**
 * 解密sm4加密的response
 * @param sm4response
 * @returns {any}
 */
export function decryptResponse(sm4response) {
    return JSON.parse(SM4.decrypt(sm4response, key, {padding: 'pkcs#7', mode: 'cbc', iv: iv}))
}